
#include "main.h"

const char num[]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

void UART_Init(void)
{
	UART1_DeInit();
	UART1_Init((u32)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, \
		UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_TXRX_ENABLE);
	UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
	UART1_Cmd(ENABLE);
}

void UART1_SendData(uint8_t Data)
{
	UART1->DR = Data;
}

void UART1_SendByte(uint8_t Data)
{
	UART1_SendData(Data);
	while (UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET);
}

void UART1_SendString(uint8_t *Data, uint8_t len)
{
	uint8_t i=0;
	for(;i<len;i++)
		UART1_SendByte(Data[i]);
}

void num2char(char *str, float number, uint8_t g, uint8_t l)
{
	uint8_t i;
	int temp = number/1;
	float t2 = 0.0;
	for (i = 1; i<=g; i++)
	{
		if (temp==0)
			str[g-i] = num[0];
		else
			str[g-i] = num[temp%10];
		temp = temp/10;
	}
	*(str+g) = '.';
	temp = 0;
	t2 = number;
	for(i=1; i<=l; i++)
	{
		temp = t2*10;
		str[g+i] = num[temp%10];
		t2 = t2*10;
	}
	*(str+g+l+1) = ' ';
	*(str+g+l+2) = '\0';
}

void uart1_rx_interrupt()
{
	UART1_ClearITPendingBit(UART1_IT_RXNE);
	UART1_SendData8(UART1_ReceiveData8());
}
